<!-- 单位编码树 -->
<template>
  <div class="tree-container">
    <div class="tree-bar">
      <el-tree
        :data="treeData"
        :props="defaultProps"
        :highlight-current="true"
        :default-expanded-keys="[this.label]"
        node-key="label"
        ref="treeBox"
        class="elTree"
        @node-click="handleNodeClick"
        lazy
        :load="loadNode"
      ></el-tree>
    </div>
  </div>
</template>

<script>
  import { getTree } from "@/api/dmgl/dwbm";
  import { getUserInfo } from "@/utils/utilInfo.js";
  import { mapGetters } from 'vuex'

  export default {
    props: {
      refresh: {
        type: String,
        default: ""
      }
    },
    data() {
      return {
        treeData: [],
        defaultProps: {
          label: "label",
          children: "child",
          isLeaf: "leaf",
        },
        label: "",
        rootDwbm: "",
        callRadioData: [], //返回的行数据
      };
    },

    components: {},

    inject: ['reload'],

    computed: {
      ...mapGetters(['czdwinfo'])
    },

    watch: {
      refresh(newVal, oldVal){
        this.treeRefuse()
      },
      czdwinfo(newVal, oldVal) {
        this.reload()
      }
    },

    created() {},

    mounted() {},

    beforeUpdate() {
      //解决列配置引发的滚动条问题
    },

    update() {},

    destroyed() {},

    methods: {
      /**点击树节点 */
      handleNodeClick(data) {
        this.$emit("handleClickTreeNode", data);
      },
      /**树的懒加载 */
      loadNode(node, resolve) {
        if (node.level === 0) {
          this.getTreeLevel(resolve);
        }else if(node.level >= 1){
          this.getNextTree(node, resolve)
        }
      },

      /**获取一级节点数据 */
      getTreeLevel(resolve) {
        const that = this
        let czdwinfo = null
        if (typeof that.czdwinfo == 'string') {
          czdwinfo = JSON.parse(that.czdwinfo)
        } else {
          czdwinfo = that.czdwinfo
        }
        if(czdwinfo.dwjb < 4 || czdwinfo.dwbm == getUserInfo.dwbm){
          this.rootDwbm = "0"
        }else {
          this.rootDwbm = czdwinfo.dwbm
        }
        let reqData = {
          dwbm: this.rootDwbm,
          root: "1"
        };
        getTree(reqData).then(res => {
          if(res.code === 0){
            that.label = res.data[0].label;
            that.treeData = res.data;
            return resolve(res.data);
          }else{
            that.$message.error(res.message)
          }
        })
      },

      /**获取下级节点数据 */
      async getNextTree(node, resolve) {
        let reqData = {
          dwbm: node.data.id,
          root: "0"
        };
        let res = await getTree(reqData)
        if(res.code === 0){
          let req = res.data
          // for (let i = 0; i < req.length; i++) {
          //     if (req[i].data.dwjb > 3) {
          //       req[i].name = "leaf";
          //       req[i].leaf = true;
          //     }
          //   }
          return resolve(req);
        }else{
          this.$message.error(res.message)
        }
      },
      /**刷新树*/
      treeRefuse(){
        const that = this
        let czdwinfo = null
        if (typeof that.czdwinfo == 'string') {
          czdwinfo = JSON.parse(that.czdwinfo)
        } else {
          czdwinfo = that.czdwinfo
        }
        if(czdwinfo.dwjb < 4 || czdwinfo.dwbm == getUserInfo.dwbm){
          this.rootDwbm = "0"
        }else {
          this.rootDwbm = czdwinfo.dwbm
        }
        let reqData = {
          dwbm: this.rootDwbm,
          root: "1"
        };
        getTree(reqData).then(res => {
          this.treeData = res.data;
        })
      }
    },
  };
</script>
<style lang='scss' scoped>
  .tree-container{
    height: 100%;
  }
  .tree-bar{
    height: 100%;
    overflow: auto;
  }
  label {
    font-weight: 400 !important;
  }
  .elTree{
    width: 100%;
    height: 100%;
    overflow: auto;
  }
  /deep/ .el-tree > .el-tree-node {
    min-width: 100%;
    display: inline-block;
  }
</style>
